<template>
    <div>
        <!--      搜索  创建-->
        <!-- <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <el-input clearable v-model="form.title" style="width: 300px" placeholder="标题"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="query">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button style="float: right;" type="success" @click="create">新增活动</el-button>
            </el-form-item>
        </el-form> -->
        <div style="float: right;margin-bottom: 10px">
            <el-button  type="success" @click="create">新增活动</el-button>
        </div>
        <!--           11 列表-->
        <el-table :data="tableData" border>
            <el-table-column
                    fixed
                    prop="originator"
                    label="发起人"
                    align="center"> 
            </el-table-column>
            <el-table-column
                    prop="theme"
                    label="活动主题"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="mode"
                    label="活动方式"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="状态"
                    align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.state == '0'">未开始</span>
                    <span v-if="scope.row.state == '1'">已开始</span>
                    <span v-if="scope.row.state == '2'">已结束</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="300px">
                <template slot-scope="scope">
                    <el-button
                            v-show="scope.row.state==='0'"
                            type="primary"
                            size="mini"
                            @click="release(scope.row)">开始
                    </el-button>
                    <el-button
                            v-show="scope.row.state!=='2'"
                            type="success"
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            v-show="scope.row.state==='2'"
                            type="success"
                            size="mini"
                            @click="detail(scope.row)">详情
                    </el-button>
                    <el-button
                            type="danger"
                            size="mini"
                            @click="handleDelete(scope.row)">删除
                    </el-button>
                    <el-button
                            v-show="scope.row.state!=='2'"
                            type="danger"
                            size="mini"
                            @click="stop(scope.row)">停止
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
<!--        分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalRecord" class="text-center mt10">
        </el-pagination>
<!--        创建弹出框-->
        <el-dialog title="创建" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="发起人" prop="originator">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.originator"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.theme"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动时间" prop="time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleForm.time"></el-input> -->
                        <el-date-picker 
                            style="width: 320px"
                            v-model="ruleForm.time"
                            type="date"  
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.address"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动方式" prop="mode">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.mode"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动目的" prop="purpose">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.purpose"></el-input>
                    </div>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </div>
        </el-dialog>

<!--        修改弹出框-->
        <el-dialog title="修改" :visible.sync="dialogFormVisibleEdit">
            <el-form :model="ruleFormEdit" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="发起人" prop="originator">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.originator"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.theme"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动时间" prop="time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleFormEdit.time"></el-input> -->
                        <el-date-picker 
                            style="width: 320px"
                            v-model="ruleFormEdit.time"
                            type="date"  
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.address"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动方式" prop="mode">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.mode"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动目的" prop="purpose">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.purpose"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>
        </el-dialog>

        <!--        详情弹出框-->
        <el-dialog title="详情" :visible.sync="detailVisible">
            <el-form :model="detailForm"  label-width="180px" class="demo-ruleForm">
                <el-form-item label="发起人" >
                    <div style="width: 320px">
                        <el-input v-model="detailForm.originator" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动主题" >
                    <div style="width: 320px">
                        <el-input v-model="detailForm.theme" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动时间" >
                    <div style="width: 320px">
                        <el-date-picker 
                            style="width: 320px"
                            v-model="detailForm.time"
                            type="date"  
                            placeholder="选择日期"
                            readonly>
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" >
                    <div style="width: 320px">
                        <el-input v-model="detailForm.address" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动方式" >
                    <div style="width: 320px">
                        <el-input v-model="detailForm.mode" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动目的" >
                    <div style="width: 320px">
                        <el-input v-model="detailForm.purpose" readonly></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>


    export default {

        name: 'test',

        data() {
            return {
                detailVisible:false,//详情弹出框
                detailForm:{},//详情数据
                tableData: [],
                pageNo: 1,
                pageSize: 10,
                totalRecord: 0,
                totalPage: 0,
                dialogFormVisible: false, //创建弹出框
                dialogFormVisibleEdit:false, //修改弹出框
                form:{},  //创建按钮的表单
                //创建表单
                ruleForm: {
                    title: '',
                    content: '',
                },
                //修改表单
                ruleFormEdit:{
                    title: '',
                    content: '',
                },
                //验证
                rules: {
                    originator: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    theme: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    mode: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    purpose: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ]
                },
                //修改验证
                rulesEdit: {
                    originator: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    theme: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    mode: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    purpose: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ]
                },
                formLabelWidth: '120px',
                cid:''
            }
        },

        props: {},

        components: {},

        computed: {},

        methods: {
            // 初始化
            async init() {
                const res = await this.$http('selectlist',{
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                })
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
                console.log(res)
            },
            handleClick(row) {
                console.log(row);
            },
            //分页
            async handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            async handleCurrentChange(val) {
                this.pageNo = val;
                this.init();
            },
            //创建
            create() {
                this.dialogFormVisible = true
                this.ruleForm.title = ""
                this.ruleForm.content = ""
            },
            //点击确定立即创建
            async sure(){
                var timestamp = this.ruleForm.time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.ruleForm.time = date
                const res = await this.$http('createservice',{
                    originator:this.ruleForm.originator,
                    theme:this.ruleForm.theme,
                    time:this.ruleForm.time,
                    address:this.ruleForm.address,
                    mode:this.ruleForm.mode,
                    purpose:this.ruleForm.purpose,
                })
                if (res.count === 1) {
                    this.$message({
                        message: '创建成功',
                        type: 'success'
                    });
                    this.dialogFormVisible = false
                    this.init()
                } else {
                    this.$message({
                        message: '创建失败',
                        type: 'error'
                    });
                }
            },
            //详情
            async detail(row){
                console.log('xinagqing',row)
                var timestamp = row.time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.detailForm.time = date
                this.detailVisible = true
                this.detailForm = row
            },
            //编辑
            handleEdit(row){
                this.dialogFormVisibleEdit = true
                this.cid = row.id
                this.ruleFormEdit = row
            },
            //点击确定即可完成编辑
            async yes(){
                var timestamp = this.ruleFormEdit.time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.ruleFormEdit.time = date
                const res = await this.$http('createservice',{
                    id:this.cid,
                    originator:this.ruleFormEdit.originator,
                    theme:this.ruleFormEdit.theme,
                    time:this.ruleFormEdit.time,
                    address:this.ruleFormEdit.address,
                    mode:this.ruleFormEdit.mode,
                    purpose:this.ruleFormEdit.purpose,
                })
                if (res.count === 1) {
                    this.$message({
                        message: '编辑成功',
                        type: 'success'
                    });
                    this.dialogFormVisibleEdit = false
                    this.init()
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error'
                    });
                }
            },
            //删除
            async handleDelete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('createservice',{
                        id:row.id,
                        del_flag:'1'
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //发布
            async release(row){
                const  res = await this.$http('updateState',{
                    id:row.id
                })
                if (res.count === 1) {
                    this.$message({
                        message: '发布成功',
                        type: 'success'
                    });
                    this.init()
                } else {
                    this.$message({
                        message: '发布失败',
                        type: 'error'
                    });
                }
            },
            //停止
            async stop(row){
                this.$confirm('将停止改活动, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('stop',{
                        id:row.id
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '停止成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            //搜索
            async query(){
                const res = await this.$http('info',{
                    title:this.form.title
                })
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            }
        },

        watch: {},

        mounted() {
            this.init();
        },

    }
</script>

<style scoped>

</style>
